<template lang="pug">
view
	view(v-if="isshow")
		u-skeleton(rows="3" title :loading="isLoading")
		u-skeleton(rows="1" :loading="isshow" rowsHeight="200")
		u-skeleton(rows="3" title :loading="isLoading")
		u-skeleton(rows="3" title :loading="isLoading")
	view(v-if="!isshow")
		view.item
			view.top
				view.name {{ address.pname }}
				view.phone {{ address.phone }}
				view.tag
					text {{ address.tag }}
			view.bottom {{address.addressname}}
		u-gap(height="10" bgColor="#eee")
		view
			view(v-for="(item,index) in orderList")
				u-row.moenyText
					u--image( :lazy-load="true" :src="item.imgurl" width="90" height="90")
					
					view.moenyText
						text ￥{{item.price}}
						text.numText &nbsp;数量:{{item.num}}&nbsp;尺码:{{item.shoesize}}
				u-gap(height="5" bgColor="#eee")
		view.buyBtn
			u-row
				u-col(span="2") &nbsp;
				u-col(span="4")
					text.allMoney 总价￥{{Math.floor(money)}}
				u-col(span="6")
					view.zhifuBtn
						u-button(type="primary" shape="circle" text="支付" @click="buy")
</template>

<script>
	/**
	 * 结算页面
	 * **/
	export default {
		data() {
			return {
				isLoading:true,
				isshow:true,
				money:0,
				address:{},
				addressList: [],
				orderList:[]
			}
		},
		methods: {
			buy(){
				// uni.showToast({
				// 	title:"谢谢老板"
				// })
				// this.orderList = []
				// this.$store.state.orderList = []
				// setTimeout(()=>{
				// 	uni.switchTab({
				// 		url:'/pages/index/index'
				// 	})
				// },1000)
				let commodityNum = 0
				for (let i = 0; i < this.orderList.length; i++) {
					commodityNum += this.orderList[i].num
				}
				let data = {
					ordernum:"DD" + Date.parse(new Date()),
					num:commodityNum,
					cidlist:JSON.stringify(this.orderList),
					aid:this.address.aid,
					price:this.money
				}
				// console.log(data);
				//批量删除的数组id
				let deletelist = []
				for (let j = 0; j < this.orderList.length; j++){
					deletelist.push(this.orderList[j].carid)
				}
				//console.log('deletelist',deletelist);
				//添加请求
				this.$store.state.orderList=[]
				this.orderList=[]
				uni.$u.http.post('/orders',data).then(res=>{
					console.log(res.data);
					if (res.data.code === 200) {
						//判断是否从购物车购买
						if(deletelist !== undefined){
							uni.$u.http.post('/cars/deleteList',deletelist).then(res=>{
								if(res.data.code === 200){
									uni.showToast({
									 	title:"提交订单成功"
									})
								}
							})
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							},1000);
						}else{
							
							setTimeout(()=>{
								uni.switchTab({
									url:'/pages/index/index'
								})
							},1000);
							
						}
					}else{
						uni.showToast({
						 	title:res.data.msg
						})
					}
				})
			},
			getAddress(){
				uni.$u.http.get('/address').then(res=>{
					this.addressList = res.data.data
					if(res.data.data.length===0){
						uni.navigateTo({
							url:"/pages/address/createAddress"
						})
					}else{
						this.address = this.addressList[0]
					console.log(res);
					setTimeout(()=>{
						this.isshow = false
					},500)
					}
					
				})
			}
		},
		onLoad() {
			
			this.orderList = this.$store.state.orderList
			for (let i = 0; i < this.orderList.length; i++) {
				let one = this.orderList[i].price * this.orderList[i].num
				this.money += one
			}
			
		},
		onShow(){
			this.orderList = this.$store.state.orderList
			for (let i = 0; i < this.orderList.length; i++) {
				let one = this.orderList[i].price * this.orderList[i].num
				this.money += one
			}
			//不知道为啥money会变成两倍 故在此除以2
			this.money = this.money/2
			
			this.getAddress()
		},
		onHide(){
			this.$store.state.orderList=[]
			this.orderList=[]
		}
	}
</script>

<style lang="scss" scoped>
	.zhifuBtn{
		margin-bottom: 15px;
	}
	.allMoney{
		display: block;
		margin-bottom: 15px;
		font-size: 34rpx;
		font-weight: 600;
	}
	.buyBtn{
		background-color: #fff;
		width: 100%;
		position: fixed;
		bottom: 0;
	}
	.numText{
		display: block;
		font-size: 28rpx;
		color: #000;
	}
	.moenyText{
		margin-left: 18px;
			color: #ffa400;
			font-weight: 600;
			font-size: 36rpx;
	}
.item {
	margin-top: 10px;
	border: 1px solid #eee;
	padding: 40rpx 20rpx;
	.top {
		display: flex;
		font-weight: bold;
		font-size: 34rpx;
		.phone {
			margin-left: 60rpx;
		}
		.tag {
			display: flex;
			font-weight: normal;
			align-items: center;
			text {
				display: block;
				width: 60rpx;
				height: 34rpx;
				line-height: 34rpx;
				color: #ffffff;
				font-size: 20rpx;
				border-radius: 6rpx;
				text-align: center;
				margin-left: 30rpx;
				background-color:rgb(49, 145, 253);
			}
			.red{
				background-color:red
			}
		}
	}
	.bottom {
		display: flex;
		margin-top: 20rpx;
		font-size: 28rpx;
		justify-content: space-between;
		color: #999999;
	}
}
.addSite {
	display: flex;
	justify-content: space-around;
	width: 600rpx;
	line-height: 100rpx;
	position: absolute;
	bottom: 30rpx;
	left: 80rpx;
	background-color: red;
	border-radius: 60rpx;
	font-size: 30rpx;
	.add{
		display: flex;
		align-items: center;
		color: #ffffff;
		.icon{
			margin-right: 10rpx;
		}
	}
}
</style>
